<template>
  <view class="search">
    <!-- 搜索框 -->
    <view class="search-box"><uni-search-bar @input="input" :radius="30" cancelButton="none" focus="true"></uni-search-bar></view>
    <!-- 搜索建议 -->
    <view class="search-help" v-if="searchHistoryList.length !== 0">
      <view class="search-help-box" v-for="(help, i) in searchList" :key="i" @click="gotoDetail(help.goods_id)">
        <view class="search-help-text">{{ help.goods_name }}</view>
        <uni-icons type="forward" size="20"></uni-icons>
      </view>
    </view>
    <view class="search-history">
      <text class="search-history-text">搜索历史</text>
      <uni-icons type="trash" size="20" @click="clearHistory"></uni-icons>
    </view>
    <view class="history-list"><uni-tag :text="history" v-for="(history, i) in historys" :key="i" @click="gotoGoodsList(history)"></uni-tag></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      time: null,
      kw: '',
      searchList: [],
      searchHistoryList: []
    };
  },
  methods: {
    input(e) {
      if (this.time !== null) {
        clearTimeout(this.time);
      }
      this.time = setTimeout(() => {
        this.kw = e;
        this.getSearchList();
      }, 500);
    },
    async getSearchList() {
      if (this.kw === '') {
        this.searchList = [];
        return;
      }
      const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw });
      if (res.meta.status !== 200) return uni.$showMsg();
      this.searchList = res.message;
      this.saveHistory();
    },
    gotoDetail(id) {
      uni.navigateTo({
        url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
      });
    },
    clearHistory() {
      this.searchHistoryList = [];
      uni.setStorageSync('kw', '[]');
    },
    saveHistory() {
      const mySet = new Set(this.searchHistoryList);
      mySet.delete(this.kw);
      mySet.add(this.kw);
      //  ... 等价于Array.from
      this.searchHistoryList = [...mySet];
      uni.setStorageSync('kw', JSON.stringify(this.searchHistoryList));
    },
    gotoGoodsList(kw) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?query=' + kw
      });
    }
  },
  computed: {
    historys() {
      return [...this.searchHistoryList].reverse();
    }
  },
  onLoad() {
    this.searchHistoryList = JSON.parse(uni.getStorageSync('kw') || '[]');
  }
};
</script>

<style lang="scss">
.search {
  height: 50px;
  background-color: #c00000;
  width: 100%;
  .search-box {
    position: sticky;
    top: 0;
    .uni-searchbar {
      padding: 10px 10px 0 10px;
    }
  }
}
.search-history {
  display: flex;
  justify-content: space-between;
  padding: 5px;
  border-bottom: 1px solid gray;
  .search-history-text {
    font-size: 14px;
  }
}
.history-list {
  margin-top: 5px;
  .uni-tag {
    margin-left: 5px;
  }
}
.search-help {
  padding: 0 5px;
  .search-help-box {
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding: 13px 0;
    font-size: 12px;
    border-bottom: 1px solid #efefef;
    .search-help-text {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
}
</style>
